<?php

namespace teamug;

/* 
 * Used by a RequestHandler to return a JSON string in a standard format.
 * 
 */

class JsonResponse {
  
  protected $obj;
  
  public function __construct() {
    $this->obj = new \stdClass;
    $this->obj->status->code = 0;
    $this->obj->status->message = '';
    $this->obj->errors = array();
    $this->obj->html = '';
    $this->obj->data = null;
  }

  
  public function __toString() {
    return json_encode($this->obj);
  }
  
  
  public function setStatus($code, $message) {
    $this->obj->status->code = $code;
    $this->obj->status->message = $message;
  }
  
  
  public function addError($code, $message) {
    $err = new stdClass;
    $err->code = $code;
    $err->message = $message;
    $this->obj->errors[] = $err;
  }
  
  
  public function setHtml($html) {
    $this->obj->html = $html;
  }
 
  
  public function setData($data) {
    $this->obj->data = $data;
  }
  
}